﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class ORDGOODSData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbWGSummary;
        public ORDGOODSData()
            : base()
        {
            BuildTrisList();
            tbWGSummary = new DataTable("WGGSUM");
            tbWGSummary.Columns.Add("ARTICLE", typeof(string));
            tbWGSummary.Columns.Add("WBBDPART", typeof(string));
            tbWGSummary.Columns.Add("FACTORY", typeof(string));
            tbWGSummary.Columns.Add("WBEWMTR", typeof(double));
            tbWGSummary.Columns.Add("PCNUM", typeof(int));
        }
        public ORDGOODSData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;21;27;32;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE, ARTICLE, CUSTOMER;
            ARTICLE = dwABPOS["ARTICLE"].ToString().Trim();
            CUSTOMER = dwABPOS["CUSTOMER"].ToString().Trim();
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "SELECT '' AS STATUS,A.VKDELDATE,A.DIFFDATE,A.VKOPENDATE,A.CUSTOMER,A.CO,A.COQTY,substr(A.ARTICLE,2,1) AS ARTICLEGROUP,A.ARTICLE, "
                    + " A.PBEAMMTR,A.SIZINGLOT,A.BEAMMTR,A.BEAMMTR-A.PBEAMMTR AS BEAMDIFF, "
                    + " A.GREIGEMTR,(1-ROUND((A.GREIGEMTR/A.BEAMMTR),4))*100||'%' AS WEAVEWASTE, "
                    + " A.FINISHMTR,(case  when a.greigemtr>0 then(1 - ROUND(A.FINISHMTR * 0.9144 / A.GREIGEMTR, 4)) * 100 || '%'  "
                    + "          else 100||'%' end) AS FINISHWASTE,A.FINISHMTR-A.COQTY AS WASTE "
                    + " FROM ICC_GOODS_DIFF A "
                    + " WHERE A.BEAMMTR >0  "
                    + " AND A.ARTICLE LIKE '%" + ARTICLE + "%' AND (A.CODE ='" + CUSTOMER + "' OR  '" + CUSTOMER + "' = '00000000') "
                    + " AND A.VKDELDATE>='" + WBSRTDATE + "' AND A.VKDELDATE<='" + WBENTDATE + "'"
                    ; 
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ORDGOODS";
                    SetDefault(tableLocal);
                    byte[] b;
                   string value; 
                   string sTemp = ""; 
                   string Column = ""; 
                   foreach (DataRow dwTemp in tableLocal.Rows)
                   {
                       Column = "CUSTOMER";
                       #region
                       b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                       value = Encoding.GetEncoding("gb2312").GetString(b);
                       dwTemp[Column] = value;
                       #endregion
                       Column = "ARTICLEGROUP";
                       value = dwTemp[Column].ToString().Trim();
                       if (value == "1")
                       {
                           dwTemp[Column] = "市场组";
                       }
                       else if (value == "2" || value == "4")
                       {
                           dwTemp[Column] = "色织组";

                       }
                       else if (value == "3")
                       {
                           dwTemp[Column] = "高档组";

                       }
                       else if (value == "6")
                       {
                           dwTemp[Column] = "差异化组";

                       }
                       else
                       {
                           dwTemp[Column] = "新塘";

                       }
                       GetABPOS(dwTemp);
                   }
                   set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region code reference

                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT A.AMSTATUS "
                + " FROM ICC_ORDER_PROCESS A "
                + " WHERE A.CO = :CO  ";

                or1.Parameters.Add("CO", OracleType.NVarChar).Value = dwABPOS["CO"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";

                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string STATUS = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "AMSTATUS";
                        value = dwTemp[Column].ToString().Trim();
                       if (value == "J")
                       {
                           STATUS = "完成";

                       }
                    }
                    dwABPOS["STATUS"] = STATUS;                    
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet BuildBABST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}